<?php
/**
 * @author Pham Van Diep
 * @copyright 2014
 * @description: save sponsor
 */
// not login
// not perm add
if(!isset($AppUI->id) ||
    !isset($_SESSION['club'])||
    !isset($_SESSION['sponsor'])||
    isset($_SESSION['sponsor']) && (($_SESSION['sponsor']&1)<< 2== 0 )||
    !isset($_POST['clubid'])    
) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
// data input
//var_dump($_POST); die();
$arrErr = array();
$intClubId = intval($_POST['clubid']);
$intId     = intval($_POST['id']);
$strName            = CI_Function::stripTag(trim($this->input->post('name',null))); 
$intLen = strlen($strName);
if ($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['name_range']);
}
$strShort    	    = CI_Function::stripTag(trim($this->input->post('sshort',null)));
$intLen = strlen($strShort);
if ($intLen < VL_SHORT_MINLENGTH || $intLen > VL_SHORT_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['short_range']);
}			
$strDes             = CI_Function::stripTag(trim($this->input->post('description',null)));
$intLen = strlen($strDes);
if ($intLen < VL_CONTENT_MINLENGTH ) {
    array_push($arrErr, $arrLocale['description_minlength']);
}
$strTag             = CI_Function::stripTag(trim($this->input->post('tag', null)));
$strLogo = trim($this->input->post('link',null));
if(isset($_FILES['logo']) && $_FILES['logo']['error'] == 0) {   
    $this->load->library('upload');
    $arrInfo = $this->upload->do_upload('logo');
    
    if(!isset($arrInfo) || count($arrInfo) == 0) {
        array_push($arrErr, $arrLocale['logo_upload']);
    }
    $strLogo = CI_Function::getImageUrl($arrInfo['path'],$arrInfo['firstname'], $arrInfo['width'], $arrInfo['height'],$arrInfo['extension']);
}

if(count($arrErr)==0) {//allow save  
	if(isset($_SESSION['SPONSOR_ERROR'])) unset($_SESSION['SPONSOR_ERROR']);
	if(isset($_SESSION['SPONSOR_INPUT'])) unset($_SESSION['SPONSOR_INPUT']);       
    $intError = $this->sponsor->update($AppUI->id,
									$intId,
									$intClubId,
									$strName,
									$strLogo,
									$strShort,
									$strDes,
									$strTag);
	if($intError == 0) CI_Function::setMsg($arrLocale['success']);    
    else if($intError == -1) array_push($arrErr,$arrLocale['system_error']);        
    else{
        array_push($arrErr,$arrError['problem_save_db_error']);        
    }            
}
// occur error

if (count($arrErr) !== 0) {     
    $_SESSION['SPONSOR_ERROR'] = $arrErr;
    $_SESSION['SPONSOR_INPUT'] = $this->input->post();
    redirect(HOST_BACKEND . 'sponsor/edit?clubid='.$intClubId);
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
